import java.util.LinkedList;

public class MyLinkedList implements IList {

    static class ListNode {
        public int val;
        public ListNode prev;//前驱
        public ListNode next;//后继

        public ListNode(int val) {
            this.val = val;
        }
    }

    public ListNode head;
    public ListNode last;


    //头插法 O(1)
    @Override
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        if (head == null) {
            head = node;
            last = node;
            return;
        }
        node.next = head;
        head.prev = node;
        head = node;
    }

    //尾插法 O(1)
    @Override
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        if (head == null) {
            head = node;
            last = node;
            return;
        }
        last.next = node;
        node.prev = last;
        last = node;
    }

    //任意位置插入,第一个数据节点为0号下标
    @Override
    public void addIndex(int index, int data) throws indexLegalityException {
        int len = size();

        //检查index是否合法性
        if (index < 0 || index > len) {
            throw new indexLegalityException("任意插入位置不合法：" + index);
            //System.out.println("任意插入位置不合法！" + index);
        }

        //头插法
        if (index == 0) {
            addFirst(data);
            return;
        }
        //尾插法
        if (index == len) {
            addLast(data);
            return;
        }

        //中间位置任意插入
        ListNode cur = findIndex(index);
        ListNode node = new ListNode(data);
        node.next = cur;
        cur.prev.next = node;
        node.prev = cur.prev;
        cur.prev = node;
    }

    /**
     * 找到 index 位置
     * @param index
     * @return
     */
    private ListNode findIndex(int index) {
        ListNode cur = head;
        while (index != 0) {
            cur = cur.next;
            index--;
        }
        return cur;
    }


    //查找是否包含关键字key是否在单链表当中
    @Override
    public boolean contains(int key) {
        ListNode cur = this.head;
        while (cur != null) {
            if (cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }


    //删除第一次出现关键字为key的节点
    //@Override
    public void remove1(int key) {
        ListNode cur = head;
        while (cur != null) {
            //开始删除了
            //进去if语句的时候，已经找到了与key相同的值
            //只是不知道相同的值是哪个节点
            //头节点
            if (cur.val == key) {
                //删除头节点
                if (cur == head) {
                    head = head.next;
                    if (head == null) {
                        last = null;
                    } else {
                        head.prev = null;
                    }
                } else {
                    //判断删除的是否是中间或尾结点
                    cur.prev.next = cur.next;
                    if (cur.next == null) {
                        //尾结点
                        last = last.prev;
                    } else {
                        //中间节点
                        cur.next.prev = cur.prev;
                    }
                    /*if (cur == last) {

                    }else {

                    }*/
                }
                return;
            } else {
                cur = cur.next;
            }
        }
    }




    @Override
    public void remove(int key) {
        if (head == null) {
            //可抛出自定义异常
            System.out.println("链表为空！");
        }
        //头节点删除,以及只有一个节点的情况
        if (head.val == key) {
            head = head.next;
            if (head == null) {
                last = null;
                return;
            }
            head.prev = null;
            return;
        }
        //尾结点删除
        if (last.val == key) {
            last.prev.next = null;
            last = last.prev;
            return;
        }

        //中间节点删除
        ListNode cur = findKey(key);
        if (cur == null) {
            return;
        } else {
            cur.prev.next = cur.next;
            cur.next.prev = cur.prev;
        }
    }
    private ListNode findKey (int key) {
        ListNode cur = this.head;
        while (cur != null) {
            if (cur.val == key) {
                return cur;
            }
            cur = cur.next;
        }
        return null;
    }


    //删除所有值为key的节点
    @Override
    public void removeAllKey(int key) {
        ListNode cur = head;
        while (cur != null) {
            //开始删除了
            //进去if语句的时候，已经找到了与key相同的值
            //只是不知道相同的值是哪个节点
            //头节点
            if (cur.val == key) {
                //删除头节点
                if (cur == head) {
                    head = head.next;
                    if (head == null) {
                        last = null;
                    } else {
                        head.prev = null;
                    }
                } else {
                    //判断删除的是否是中间或尾结点
                    cur.prev.next = cur.next;
                    if (cur.next == null) {
                        //尾结点
                        last = last.prev;
                    } else {
                        //中间节点
                        cur.next.prev = cur.prev;
                    }
                }
            }
            cur = cur.next;
        }
    }





    //得到单链表长度
    @Override
    public int size() {
        ListNode cur = this.head;
        int len = 0;
        while (cur != null) {
            len++;
            cur = cur.next;
        }
        return len;
    }

    @Override
    public void clear() {
        ListNode cur = head;
        while (cur != null) {
            ListNode curNext = cur.next;
            cur.val = 0;
            cur.prev = null;
            cur.next = null;
            cur = curNext;
        }
        head = null;
        last = null;
    }

    //打印链表(遍历链表)
    @Override
    public void display() {
        ListNode cur = this.head;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }
}
